<template>
  <!-- 放置弹层组件 -->
  <!-- @open="getEmployeesSimle" -->
  <!-- close-on-click-modal点击其弹窗其他地方也不会让弹窗消失 -->
  <el-dialog
    :title="showTitle"
    :visible="showDialog"
    :close-on-click-modal="false"
    @close="closeDialogFn"
    @open="getEmployeesSimle"
  >
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <!--
      1.el-form :model
      2.el-form :rules
      3.el-form-item prop
      4.el-input v-model
     -->
    <el-form ref="addForm" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button type="primary" size="small" @click="confirmAddDept">确定</el-button>
      <el-button size="small" @click="closeDialogFn">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getEmployeesSimleApi } from '@/api/employees'
import { addDepartmentApi, editDepartmentList, editDepartmentApi } from '@/api/departments'
export default {
  name: 'Sdddept',
  // 需要传入一个props变量来控制 显示或者隐藏
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      default: () => { }
    },
    departList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      form: {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不可为空', trigger: 'blur' },
          { min: 1, max: 10, message: '部门名称长度为1-10', trigger: 'blur' },
          { validator: this.validateDeptName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不可为空', trigger: 'blur' },
          { min: 1, max: 10, message: '部门编码长度为1-10', trigger: 'blur' },
          { validator: this.validateDeptCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不可为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门介绍不可为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍长度为1-300', trigger: 'blur' }
        ]
      },
      managerList: []
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    // 回显
    async editDepartmentFn() {
      const res = await editDepartmentList(this.nodeData.id)
      console.log(res)
      // this.form = data
      this.form = res.data
    },
    async getEmployeesSimle() {
      const { data } = await getEmployeesSimleApi()
      this.managerList = data
    },
    closeDialogFn() {
      // 子组件必须通过固定语法来修改父组件放权过来的值
      this.$emit('update:showDialog', false)
      this.form = {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      }
      // 清空数据
      this.$refs.addForm.resetFields()
    },

    // 校验部门名称

    validateDeptName(rule, value, callback) {
      // 要拿输入的内容和点击部门的子部门进行对比
      // console.log(value)
      // console.log(this.nodeData.children)
      let children = []
      if (this.form.id) {
        if (this.nodeData.name === value) {
          callback()
          return
        } else {
          children = this.departList.filter(item => item.pid === this.nodeData.pid)
        }
      } else {
        children = this.nodeData.children || []
      }

      const flag = children.some(item => item.name === value)
      flag ? callback(new Error('部门名称不能重复')) : callback()
    },
    // 校验部门编码
    validateDeptCode(rule, value, callback) {
      if (this.form.id && value === this.nodeData.code) {
        console.log(this.form.id)
        console.log(this.nodeData.code)
        callback()// 不会阻碍后面代码的执行
      // 可加可不加因为后面是else只会执行if里面的
      } else {
        const flag = this.departList.some(item => item.code === value)
        flag ? callback(new Error('部门编码不能重复')) : callback()
      }
      // console.log(value)
      // console.log(this.departList)
    },
    /**
     * 添加
     */
    confirmAddDept() {
      this.$refs.addForm.validate(async valid => {
        if (!valid) return
        if (this.form.id) {
          await editDepartmentApi(this.form)

          this.$message.success('恭喜!修改成功')
        } else {
          this.form.pid = this.nodeData.id
          await addDepartmentApi(this.form)
          this.$message.success('恭喜!添加成功')
        }
        this.closeDialogFn()
        // this.$emit('reloadList')
        // this.$parent 上层组件 在父组件中调用子组件 一定把子组件放到最外层
        this.$parent.getDepartmentList()
      })
    }
  }
}
</script>
